﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using VisionDesigner;

namespace ImageRenderControl
{
    public partial class VisionDisplayControl : UserControl
    {

        // 新增的层叠窗口（独立顶级窗口）
        public LayeredOverlayForm overlayForm;

        // 导入 SetParent API，使 overlayForm 成为 VisionDisplayControl 的子窗口
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        public VisionDisplayControl()
        {
            InitializeComponent();
            overlayForm = new LayeredOverlayForm(this);
            overlayForm.Show();
            // 将 overlayForm 设置为当前控件的子窗口
            SetParent(overlayForm.Handle, Handle);
            PositionOverlay();

            Load += VisionDisplayControl_Load;
            SizeChanged += (s, e) => PositionOverlay();
            LocationChanged += (s, e) => PositionOverlay();
        }

        private void VisionDisplayControl_Load(object sender, EventArgs e)
        {
            //overlayForm = new LayeredOverlayForm(this);
            //overlayForm.Show();
            //// 将 overlayForm 设置为当前控件的子窗口
            //SetParent(overlayForm.Handle, Handle);
            //PositionOverlay();
        }

        /// <summary>
        /// 将 overlayForm 定位到内部 MVDRender 控件所在位置
        /// </summary>
        private void PositionOverlay()
        {
            // MVDRender 为 VisionDisplayControl 内部一个用于渲染的控件（需在设计器中定义）
            if (overlayForm != null && MVDRender != null)
            {
                overlayForm.Bounds = MVDRender.Bounds;
            }
        }

        // 对外接口：添加图形到 overlay 中
        public void AddOverlayShape(Shape shape)
        {
            overlayForm?.AddShape(shape);
        }


        #region 坐标转换
        /// <summary>
        /// 将 MVDRender 窗口坐标转换为 LayeredOverlayForm 坐标
        /// </summary>
        public void MVDWindowToOverlay(int mvdWindowX, int mvdWindowY, out int overlayX, out int overlayY)
        {
            System.Drawing.Point screenPoint = MVDRender.PointToScreen(new System.Drawing.Point(mvdWindowX, mvdWindowY));
            System.Drawing.Point overlayPoint = overlayForm.PointToClient(screenPoint);

            overlayX = overlayPoint.X;
            overlayY = overlayPoint.Y;
        }

        /// <summary>
        /// 将 LayeredOverlayForm 坐标转换为 MVDRender 窗口坐标
        /// </summary>
        public void OverlayToMVDWindow(int overlayX, int overlayY, out int mvdWindowX, out int mvdWindowY)
        {
            System.Drawing.Point screenPoint = overlayForm.PointToScreen(new System.Drawing.Point(overlayX, overlayY));
            System.Drawing.Point mvdPoint = MVDRender.PointToClient(screenPoint);

            mvdWindowX = mvdPoint.X;
            mvdWindowY = mvdPoint.Y;
        }

        /// <summary>
        /// 将 LayeredOverlayForm 坐标转换为图像坐标
        /// </summary>
        public void OverlayToImage(int overlayX, int overlayY, out int imageX, out int imageY)
        {
            int mvdWindowX, mvdWindowY;
            OverlayToMVDWindow(overlayX, overlayY, out mvdWindowX, out mvdWindowY);

            float tempImageX = 0, tempImageY = 0;
            MVDRender.TransformCoordinate(mvdWindowX, mvdWindowY, ref tempImageX, ref tempImageY, MVDCoordTransType.Wnd2Img);

            imageX = (int)tempImageX;
            imageY = (int)tempImageY;
        }

        /// <summary>
        /// 将图像坐标转换为 LayeredOverlayForm 坐标
        /// </summary>
        public void ImageToOverlay(int imageX, int imageY, out int overlayX, out int overlayY)
        {
            float mvdWindowX = 0, mvdWindowY = 0;
            MVDRender.TransformCoordinate(imageX, imageY, ref mvdWindowX, ref mvdWindowY, MVDCoordTransType.Img2Wnd);

            MVDWindowToOverlay((int)mvdWindowX, (int)mvdWindowY, out overlayX, out overlayY);
        }


        #endregion

    }
}
